package com.cater.project.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.project.entity.DemandCommunicate;
import com.cater.project.service.DemandCommunicateServiceImpl;
import com.cater.project.service.DemandEnterpriseServiceImpl;
import com.cater.query.CommonQuery;
import com.cater.report.util.RemindDateUtils;
import com.cater.sys.entity.Company;
import com.cater.sys.service.impl.CompanyServiceImpl;
import com.cater.talents.entity.Resume;
import com.cater.talents.entity.TalentsRecommend;
import com.cater.talents.entity.WorkSearch;
import com.cater.talents.service.ResumeProjectServiceImpl;
import com.cater.talents.service.ResumeServiceImpl;
import com.cater.talents.service.TalentsRecommendServiceImpl;
import com.cater.talents.service.WorkSearchServiceImpl;
import com.cater.task.service.TaskCompletionServiceImpl;
import com.cater.task.service.TaskManagerServiceImpl;
import com.cater.utils.StringUitl;

/**
 * 描述：沟通记录控制层
 * 
 * @author yong
 * @date 2018/06/11
 */
@Controller
@RequestMapping("/demandCommunicate")
public class DemandCommunicateController {

	@Autowired
	private DemandCommunicateServiceImpl service;
	@Autowired
	private ResumeServiceImpl resumeService;// 简历
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Autowired
	private CompanyServiceImpl companyService;// 公司
	@Autowired
	private DemandEnterpriseServiceImpl demandService;// 需求
	@Autowired
	private TaskManagerServiceImpl taskManagerService;// 任务
	@Autowired
	private TaskCompletionServiceImpl taskComletionService;// 任务完成情况
	@Autowired
	private TalentsRecommendServiceImpl talentsRecommendService;// 推荐
	@Autowired
	private ResumeProjectServiceImpl resumeProjectService;// 简历项目关联
	@Autowired
	private WorkSearchServiceImpl workSearchService;// 工作记录

	/**
	 * @沟通记录管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list(CommonQuery query, Model model) {
		String recommendId = query.getRecommendId();
		if (StringUtils.isNotEmpty(recommendId)) {
			TalentsRecommend recommend = talentsRecommendService.getById(recommendId);
			if (recommend != null) {
				model.addAttribute("recommend", recommend);
			}
		}
		String talentsId = query.getTalentsId();
		if (StringUtils.isNotEmpty(talentsId)) {
			model.addAttribute("talentsId", talentsId);
		}
		String workId = query.getWorkId();
		if (StringUtils.isNotEmpty(workId)) {
			model.addAttribute("workId", workId);
		}
		return "project/demandCommunicate/list";
	}

	/**
	 * @沟通记录管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询沟通记录分页数据")
	public PageResult<DemandCommunicate> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<DemandCommunicate> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("demandCommunicate:add")
	public String addView(String talentsId, String demandId, String companyId, String recommendId, Model model,
			CommonQuery query) {
		if (StringUtils.isNotEmpty(talentsId)) {
			query.setTalentsId(talentsId);
			model.addAttribute("telentsId", query.getTalentsId());
			List<DemandCommunicate> list = service.getListBytelentsId(query);
			model.addAttribute("chatlist", list);
		}
		if (StringUtils.isNotEmpty(demandId)) {
			model.addAttribute("demandId", demandId);
		}
		if (StringUtils.isNotEmpty(recommendId)) {
			model.addAttribute("recommendId", recommendId);
		}
		String communicateId = query.getCommunicateId();
		if (StringUtils.isNotEmpty(communicateId)) {
			DemandCommunicate communicate = service.getById(communicateId);
			if (communicate != null) {
				communicate.setIsPast(1);
				service.update(communicate);
			}
		}
		model.addAttribute("companyId", companyId);
		return "project/demandCommunicate/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("demandCommunicate:add")
	@ResponseBody
	@SystemControllerLog(description = "添加沟通记录")
	public JSONObject add(DemandCommunicate t, CommonQuery query, String details) {
		JSONObject json = new JSONObject();
		String resumeId = t.getTelentsId();
		String projectId = t.getProjectId();
		String recommendId = t.getRecommendId();
		String classid = t.getClassid();
		String companyId = query.getCompanyId();
		String companyName = "";
		String phone = "";
		String email = "";
		String postition = "";
		WorkSearch workSearch = new WorkSearch();
		try {
			if (StringUtils.isNotEmpty(recommendId)) {
				TalentsRecommend recommend = talentsRecommendService.getById(recommendId);
				if (recommend != null) {
					recommend.setStatus(classid);
					talentsRecommendService.update(recommend);
					companyId = recommend.getCompanyId();
					if (StringUtils.isNotEmpty(companyId)) {
						Company company = companyService.getById(companyId);
						if (company != null) {
							companyName = company.getName();
							postition = recommend.getPosts();
						}
					}
				}
			}
			if (StringUtils.isNotEmpty(resumeId)) {
				Resume resume = resumeService.getById(resumeId);
				if (resume != null) {
					t.setTrueName(resume.getTrueName());
					resume.setStatus(t.getClassid());// 修改简历目前状态
					resumeService.update(resume);
					workSearch.setTalentsId(resumeId);
					workSearch.setTrueName(resume.getTrueName());
					String sex = "";
					if (resume.getSex() == 1) {
						sex = "男";
					} else {
						sex = "女";
					}
					workSearch.setSex(sex);
					String birthdate = resume.getBirthdate();
					String age = "0";
					if (StringUtils.isNotEmpty(birthdate)) {
						SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
						Date bithday = format.parse(birthdate);
						age = StringUitl.getAgeByBirth(bithday) + "岁";
					}
					workSearch.setAge(age);
					phone = resume.getCellphone();
					workSearch.setContact(phone + " " + resume.getLifephone());
					email = resume.getEmail();
					workSearch.setEmail(email);
				}
			}
			String id = service.saves(t);
			if (StringUtils.isNotBlank(id)) {
				workSearch.setCompanyId(companyId);
				workSearch.setCompanyName(companyName);
				workSearch.setPostition(postition);
				query.setPhone(phone);
				query.setEmail(email);
				WorkSearch workold = workSearchService.checkUnique(query);
				if (workold == null) {
					workSearchService.saves(workSearch);
				} else {
					DemandCommunicate demandCommunicate = service.getById(id);
					if (demandCommunicate != null) {
						demandCommunicate.setWorkId(workold.getId());
						service.update(demandCommunicate);
					}
				}
				json.put("code", 200);
				json.put("message", "success");
			} else {
				json.put("code", 999);
				json.put("message", "fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "fail");
		}

		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("demandCommunicate:edit")
	public String editView(String id, String talentsId, Model model, CommonQuery query) {
		if (StringUtils.isNotEmpty(talentsId)) {
			query.setTalentsId(talentsId);
			model.addAttribute("telentsId", query.getTalentsId());
			List<DemandCommunicate> list = service.getListBytelentsId(query);
			model.addAttribute("chatlist", list);
		}
		return "project/demandCommunicate/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("demandCommunicate:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改沟通记录")
	public JSONObject update(DemandCommunicate t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String resumeId = t.getTelentsId();
		if (StringUtils.isNotEmpty(resumeId)) {
			Resume resume = resumeService.getById(resumeId);
			resume.setStatus(t.getClassid());// 修改简历目前状态
			resumeService.update(resume);
		}
		String id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("demandCommunicate:delete")
	@SystemControllerLog(description = "删除沟通记录")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 头部消息提醒
	 * 
	 * @param query
	 * @return
	 */
	@RequestMapping("/msg")
	@ResponseBody
	public JSONObject getCommunicateList(CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			Date wsdate = RemindDateUtils.getCurrentDayStartTime();
			Date wedate = RemindDateUtils.getCurrentDayEndTime();
			query.setStartDate(sdf.format(wsdate));
			query.setEndDate(sdf.format(wedate));
			List<Map<String, String>> maps = service.getCommunicateForMsg(query);
			json.put("msgs", maps);
			json.put("count", maps.size());
			json.put("code", 200);
			json.put("message", "消息获取成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "消息获取失败");
		}
		return json;
	}

	/**
	 * 根据推荐ID获取数据
	 * 
	 * @param recommendId
	 * @return
	 */
	@RequestMapping("/getByRecommend")
	public String getByRecommend(CommonQuery query, Model model) {
		String recommendId = query.getRecommendId();
		String workId = query.getWorkId();
		List<DemandCommunicate> communicates = null;
		try {
			if (StringUtils.isNotEmpty(recommendId)) {
				communicates = new ArrayList<DemandCommunicate>();
				communicates = service.getByRecommandId(recommendId);
				TalentsRecommend recommend = talentsRecommendService.getById(recommendId);
				if (recommend != null) {
					model.addAttribute("recommend", recommend);
				}
			}
			if (StringUtils.isNotEmpty(workId)) {
				communicates = new ArrayList<DemandCommunicate>();
				communicates = service.getListBy(query);
			}
			if (communicates != null && communicates.size() > 0) {
				model.addAttribute("communicates", communicates);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "project/demandCommunicate/recommendInfo";
	}

	@RequestMapping(value = "addForDemand")
	public String addForDemand(String talentsId, String demandId, String companyId, String recommendId, Model model,
			CommonQuery query) {
		if (StringUtils.isNotEmpty(talentsId)) {
			query.setTalentsId(talentsId);
			model.addAttribute("telentsId", query.getTalentsId());
			List<DemandCommunicate> list = service.getListBytelentsId(query);
			model.addAttribute("chatlist", list);
		}
		if (StringUtils.isNotEmpty(demandId)) {
			model.addAttribute("demandId", demandId);
		}
		if (StringUtils.isNotEmpty(recommendId)) {
			model.addAttribute("recommendId", recommendId);
		}
		model.addAttribute("companyId", companyId);
		return "project/demandCommunicate/add_demand";

	}

	/**
	 * 
	 * @Description:沟通是否通过
	 * @param query
	 * @return JSONObject
	 * @time:2019年4月27日 上午11:54:47
	 */
	@RequestMapping("/updateStatus")
	@ResponseBody
	public JSONObject UpdateStatus(String id, Integer ispast) {
		JSONObject json = new JSONObject();
		try {
			if (StringUtils.isNotEmpty(id)) {
				DemandCommunicate communicate = service.getById(id);
				if (communicate != null) {
					communicate.setIsPast(ispast);
					service.update(communicate);
					json.put("code", 200);
					json.put("message", "状态修改成功");
				}
			} else {
				json.put("code", 400);
				json.put("message", "数据不存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 500);
			json.put("message", "状态修改异常");
		}
		return json;
	}

}